package com.fjec.paid.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjec.common.util.DateUtil;
import com.fjec.common.util.SequenceIdGenerator;
import com.fjec.paid.entity.BizOrderPayment;
import com.fjec.paid.entity.BizYsPayment;
import com.fjec.paid.entity.BizYsPaymentLog;
import com.fjec.paid.mapper.BizOrderPaymentMapper;
import com.fjec.paid.rabbitmq.provider.PaymentReceiptProvider;
import com.fjec.paid.service.IBizOrderPaymentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.paid.service.IBizYsPaymentLogService;
import com.fjec.paid.service.IBizYsPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * <p>
 * 001.订单支付表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-11-09
 */
@Service
public class BizOrderPaymentServiceImpl extends ServiceImpl<BizOrderPaymentMapper, BizOrderPayment> implements IBizOrderPaymentService {

    @Resource
    private BizOrderPaymentMapper mapper;

    @Resource
    private IBizYsPaymentLogService bizYsPaymentLogService;

    @Resource
    private IBizYsPaymentService bizYsPaymentService;

    @Autowired
    private PaymentReceiptProvider paymentReceiptProvider;

    @Autowired
    private SequenceIdGenerator idGenerator;

    @Override
    public Map<String, Object> getPreparePayPackage(Long orderId) {
        return mapper.getPreparePayPackage(orderId);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void payNotify(String notifyTime, String outTradeNo, String totalAmount,
                          String tradeNo, String tradeStatus, String accountDate, Integer paymentType) throws ParseException {
        // 查询是否存在此支付
        BizYsPayment bizYsPayment = bizYsPaymentService.getOne(
                new QueryWrapper<BizYsPayment>()
                        .eq("OutTradeNo", outTradeNo)
                        .eq("TradeNo", tradeNo)
                        .eq("PaidStatus", 1)
                        .eq("TradeStatus", "WAIT_BUYER_PAY"));
        if (bizYsPayment == null)
            return;
        // 银盛订单支付异步表
        BizYsPaymentLog bizYsPaymentLog = new BizYsPaymentLog();
        bizYsPaymentLog.setId(idGenerator.nextId());
        bizYsPaymentLog.setYsPaymentId(bizYsPayment.getId());
        bizYsPaymentLog.setPaymentId(bizYsPayment.getPaymentId());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        bizYsPaymentLog.setNotifyTime(formatter.parse(notifyTime).getTime());
        bizYsPaymentLog.setOutTradeNo(outTradeNo);
        bizYsPaymentLog.setTradeNo(tradeNo);
        bizYsPaymentLog.setTradeStatus(tradeStatus);
        bizYsPaymentLog.setCreateTime(DateUtil.nowLdt());
        bizYsPaymentLogService.save(bizYsPaymentLog);

        // 更新 银盛订单支付表
        Long orderId = Long.parseLong(outTradeNo.substring(8));
        bizYsPayment.setTradeStatus(tradeStatus);
        if (paymentType == 2 && tradeStatus.equals("WAIT_SELLER_SEND_GOODS")) {
            // 担保交易，实物订单
            bizYsPayment.setPaidStatus(2);
            this.setPaymentStatus(orderId, 3);
            // 发送订单支付成功消息到MQ, 更新订单表订单状态
            paymentReceiptProvider.sendPaymentReceipt(bizYsPayment.getPaymentId(), 2, "TRADE_SUCCESS", orderId, paymentType);
        } else if (paymentType == 1 && tradeStatus.equals("TRADE_SUCCESS")) {
            // 即时到账交易，平台虚拟订单
            bizYsPayment.setPaidStatus(2);
            this.setPaymentStatus(orderId, 3);
            // 发送订单支付成功消息到MQ, 更新订单表订单状态
            paymentReceiptProvider.sendPaymentReceipt(bizYsPayment.getPaymentId(), 2, "TRADE_SUCCESS", orderId, paymentType);
        } else if (tradeStatus.equals("TRADE_CLOSED")) {
            bizYsPayment.setPaidStatus(3);
            this.setPaymentStatus(orderId, 2);
        }
        bizYsPaymentService.updateById(bizYsPayment);
    }

    @Override
    public void setPaymentStatus(Long orderId, Integer paymentStatus) {
        mapper.setPaymentStatus(orderId, paymentStatus);
    }

    @Override
    public Map<String, Object> getPaymentByOrderId(Long orderId) {
        return mapper.getPaymentByOrderId(orderId);
    }
}
